import {
	httpTokenRequest
} from '@/utils/http.js';
export default {
	data() {
		return {
			menuList: [{
				icon: '/static/img/wenTiKu/wx.png',
				name: '微信好友',
				remark: '',
				url: '',
				handler: 'shareTo'
			},
			{
				icon: '/static/img/wenTiKu/pyq.png',
				name: '朋友圈',
				remark: '',
				url: '',
				handler: 'shareToMoments'
			},
			{
				icon: '/static/img/wenTiKu/qq.png',
				name: 'QQ好友',
				remark: '',
				url: '',
				handler: 'toShareQq'
			},
			{
				icon: '/static/img/wenTiKu/copyLink.png',
				name: '复制链接',
				remark: '',
				url: '',
				handler: 'copyLink'
			},
			{
				icon: '/static/img/wenTiKu/collect_0.png',
				name: '收藏',
				remark: '',
				url: '',
				handler: 'collect'
			},
			],
			details: {},
			sourceList: [],
			showMore: false,
			shareId: null,
		};
	},
	methods: {
		//获取详细信息
		getInfo(id) {
			httpTokenRequest({
				url: this.$paths.getinfoIssue,
				method: 'POST',
				params: {
					id: id
				}
			})
				.then((res) => {
					if (res.code == 1) {
						this.details = res.data;
						this.sourceList = res.data.all_based_source;
					} else {
						this.$tools.showToast(res.msg);
					}
				})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},

		//点击前往法规库
		goSource(item) {
			if (item.type == 1) {

				uni.navigateTo({
					url: '../../lawBank/revenue/revenueInfo?id=' + item.id
				});
			} else if (item.type == 2) {
				uni.navigateTo({
					url: '../../lawBank/accountant/accountantInfo?id=' + item.id
				});
			}
		},

		//关闭弹框
		close() {
			this.showMore = false;
		},

		//点击更多中的菜单
		menuClick(item) {
			this.menuList[4].icon = `/static/img/wenTiKu/collect_${this.details.is_collect}.png`;
			if (item.handler) {
				this[item.handler]();
			} else if (item.url) {
				this.$used.href(item.url);
			}
		},

		//微信好友
		shareTo() {
			var that = this
			let code = uni.getStorageSync('userInfo').invite_code;
			let obj = {
				id: 'gh_d8bf9808c336',
				path: '/pages/details/wentikuInfo?id=' + that.shareId + '&code=' + code,
				webUrl: that.$h5Url.server + '/pages/downLoad/downLoad'
			};
			uni.share({
				provider: 'weixin',
				scene: 'WXSceneSession',
				type: 5,
				miniProgram: obj,
				imageUrl: '/static/logo-min.png',
				title: that.details.title,
				success: function (res) {
					console.log('success:' + JSON.stringify(res));
					that.showMore = false;
				},
				fail: function (err) {
					console.log('fail:' + JSON.stringify(err));
				}
			});
		},

		//分享朋友圈
		shareToMoments() {
			var that = this
			var share_url = this.$h5Url.server + '/pages/details/wenTiKuInfo?id=' + this.shareId;
			uni.share({
				provider: 'weixin',
				scene: 'WXSceneTimeline',
				type: 0,
				href: share_url,
				title: that.details.title,
				summary: that.removeTextTag(),
				imageUrl: '/static/logo-min.png',
				success: function (res) {
					console.log('success:' + JSON.stringify(res));

				},
				fail: function (err) {
					console.log('fail:' + JSON.stringify(err));
				}
			});
		},

		//分享朋友圈
		// shareToMoments() {
		// 	var that = this
		// 	let code = uni.getStorageSync('userInfo').invite_code;
		// 	let obj = {
		// 		id: 'gh_d8bf9808c336',
		// 		path: '/pages/details/wentikuInfo?id=' + that.shareId + '&code=' + code,
		// 		webUrl: that.$h5Url.server + '/pages/downLoad/downLoad'
		// 	};
		// 	uni.share({
		// 		provider: 'weixin',
		// 		scene: 'WXSceneTimeline',
		// 		type: 5,
		// 		miniProgram: obj,
		// 		imageUrl: '/static/logo-min.png',
		// 		title: that.details.title,
		// 		success: function (res) {
		// 			console.log('success:' + JSON.stringify(res));

		// 		},
		// 		fail: function (err) {
		// 			console.log('fail:' + JSON.stringify(err));
		// 		}
		// 	});
		// },

		//分享QQ
		toShareQq() {
			var that = this
			var share_url = this.$h5Url.server + '/pages/details/wenTiKuInfo?id=' + this.shareId;
			uni.share({
				provider: 'qq',
				scene: 'WXSceneSession',
				type: 0,
				href: share_url,
				title: that.details.title,
				summary: that.removeTextTag(),
				imageUrl: '/static/logo-min.png',
				success: function (res) {
					console.log('success:' + JSON.stringify(res));
					that.showMore = false;
				},
				fail: function (err) {
					console.log('fail:' + JSON.stringify(err));
				}
			});
		},


		//复制链接
		copyLink() {
			var that = this;
			uni.setClipboardData({
				data: that.$h5Url.server + '/pages/details/wenTiKuInfo?id=' + that.shareId,
				success: function () {
					uni.showToast({
						title: '链接复制成功'
					});
					that.showMore = false;
					uni.showToast({
						title: '链接复制成功'
					});
				}
			});
		},


		//收藏 or 取消收藏
		collect() {
			httpTokenRequest({
				method: 'POST',
				url: this.$paths.questionInfoCollect,
				params: {
					id: this.details.id
				}
			})
				.then((res) => {
					if (res.code == 1) {
						//收藏和未收藏切换
						this.details.is_collect = 1 - this.details.is_collect;
						console.log(this.details.is_collect);
						this.menuList[4].icon = `/static/img/wenTiKu/collect_${this.details.is_collect}.png`;
						if (this.details.is_collect == 1) {
							this.$tools.showToast('收藏成功');
						} else if (this.details.is_collect == 0) {
							this.$tools.showToast('取消收藏');
						}
						setTimeout(() => {
							this.showMore = false;
						}, 1000);
					} else {
						this.$tools.showToast(res.msg);
					}
				})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},

		//去纠错
		toRectify() {
			uni.navigateTo({
				url: './rectify?type=1&id=' + this.shareId
			});
		},

		//格式化富文本内容
		removeTextTag() {
			const regex = /<[^<>]+>/g;
			const nbspRegex = /&nbsp;/gi;
			return this.details.answer_content.replace(regex, '').replace(nbspRegex, '');
		},
	},
	onLoad(e) {
		this.getInfo(e.id);
		this.shareId = e.id;
	},
}